#region License
///License Begin
///This program has been developed by: Rodolfo Grave de Peralta, 
///Copyright (C) 2007 Rodolfo Grave de Peralta
///This program is free software; you can redistribute it and/or modify
///it under the terms of the GNU General Public License as published by
///the Free Software Foundation; either version 2 of the License, or
///(at your option) any later version.
///
///This program is distributed in the hope that it will be useful,
///but WITHOUT ANY WARRANTY; without even the implied warranty of
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///GNU General Public License for more details.
///
///You should have received a copy of the GNU General Public License
///along with this program; if not, write to the Free Software
///Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
///License End
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using TEAM.TimeTracking.Model;

namespace TEAM.TimeTracking.View
{
    /// <summary>
    /// Interaction logic for ConfirmDefaultTaskWindow.xaml
    /// </summary>

    public partial class ConfirmDefaultTaskWindow : System.Windows.Window
    {

        public ConfirmDefaultTaskWindow()
        {
            InitializeComponent();
        }

        public void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        protected void Window_KeyDown(object sender, KeyboardEventArgs e)
        {
            if (e.KeyboardDevice.IsKeyDown(Key.Enter))
            {
                ConfirmDefaultAction();
            }
            else if (e.KeyboardDevice.IsKeyDown(Key.Escape))
            {
                DontChangeTask();
            }
        }

        protected int state;

        public bool IsArriving
        {
            get
            {
                return state == 1;
            }
            set
            {
                state = value ? 1 : 0;
                UpdateVisualState();
            }
        }

        public bool IsLeaving
        {
            get
            {
                return state == 2;
            }
            set
            {
                state = value ? 2 : 0;
                UpdateVisualState();
            }
        }

        protected void UpdateVisualState()
        {
            ctrlExitContainer.Visibility = IsLeaving ? Visibility.Visible : Visibility.Collapsed;
            UpdateActionText();
        }

        protected void UpdateActionText()
        {
            ctrlLeavingText.Visibility = IsLeaving ? Visibility.Visible : Visibility.Collapsed;
            ctrlArrivingText.Visibility = IsArriving ? Visibility.Visible : Visibility.Collapsed;
        }

        protected void Task_Click(object sender, RoutedEventArgs e)
        {
            Task t = ((Button)e.Source).DataContext as Task;
            DataModel model = DataContext as DataModel;

            if (t != null && model != null)
            {
                model.CurrentUser.StartTask(t, DateTime.Now, ctrlDescription.Text);
                Close();
            }
            else
            {
                MessageBox.Show("Error: the model or the task were null");
            }
        }

        protected void ConfirmDefaultAction()
        {
            DataModel model = DataContext as DataModel;
            if (model == null)
            {
                MessageBox.Show("ERROR: DataContext was lost");
            }
            else
            {
                if (IsLeaving)
                {
                    model.CurrentUser.StartTask(model.CurrentUser.DefaultTaskWhenLeaving, DateTime.Now, ctrlDescription.Text);
                }
                else if (IsArriving)
                {
                    model.CurrentUser.StartTask(model.CurrentUser.DefaultTaskWhenArriving, DateTime.Now, ctrlDescription.Text);
                }
                App.CurrentApp.Database.Set(model.CurrentUser.History);
            }
            Close();
        }

        protected void DontChangeTask()
        {
            User currentUser = App.CurrentApp.CurrentModel.CurrentUser;
            if (currentUser.IsExecutingTask)
            {
                currentUser.ContinueCurrentTask();
                Close();
            }
            else
            {
                ctrlAllTasks.IsExpanded = true;
                MessageBox.Show("Please, select some task");
            }
        }

        protected void Cancel()
        {
            Hide();
        }

        protected void Yes_Click(object sender, RoutedEventArgs e)
        {
            ConfirmDefaultAction();
        }

        protected void NotChangingTask_Click(object source, RoutedEventArgs e)
        {
            DontChangeTask();
        }

        protected void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Cancel();
        }

    }
}